// 隐藏按钮
var hideBtn = document.querySelector(".hideBtn");
var hideMain = document.querySelector(".sidebarText");
var author = document.querySelector(".author");

var isHide = true;
var hidFun = () => {
    if (isHide) {
        hideMain.style.display = "none";
        hideBtn.style.left = "0.5rem";
        hideBtn.innerHTML = "显示";
        author.style.display = "none";
    } else {
        hideMain.style.display = "block";
        let leftLength = hideMain.offsetWidth/16 + 0.5;
        hideBtn.style.left = `${leftLength}rem`;
        hideBtn.innerHTML = "隐藏";
        author.style.display = "block";
    }
}
// hidFun();
hideBtn.onclick = function () {
    isHide = !isHide;
    hidFun();
}

var write = document.querySelector("#write").childNodes;

//渲染子节点
var list = [];
write.forEach((item,index)=>{   
    if (
        item.localName == 'h1' || item.localName == 'h2' || item.localName == 'h3' || item.localName == 'h4' || item.localName == 'h5' || item.localName == 'h6'
        ) {
        let title = "";
        for (let i = 1; i < item.childNodes.length; i++) {
            title = title + item.childNodes[i].innerHTML;
        }
        list.push({
            text: title,
            a: item.childNodes[0].name,
            tag:item.nodeName
        })
    }
    
})

list.forEach((item, index) => {
    let sideH1 = document.createElement("div");
    sideH1.setAttribute("class", `side`+`${item.tag}`);
    sideH1.setAttribute("id", `${item.a}`);
    sideH1.innerHTML = item.text;
    hideMain.appendChild(sideH1);
})

// 点击事件
hideMain.onclick = (event) => {
    if (event.target.className != "sidebarText") {
        let jumpArr = document.querySelector(`a[name=` + `"${event.target.id}"` + `]`);
        jumpArr.scrollIntoView();
    }
}